package cn.youyeku.zsxh.Utils;
import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.os.Bundle;



import java.util.Collections;
import java.util.LinkedList;
import java.util.List;


public abstract class AbsSuperApplication extends Application {

    /**
     * 维护Activity 的list
     * final的意思是地址不可修改
     * 管理act的活动
     */

    private  static List<Activity> mActivitys = Collections.synchronizedList(new LinkedList<Activity>());

    @Override
    public void onCreate() {
        super.onCreate();

        registerActivityListener();
    }




    public void pushActivity(Activity activity) {
        mActivitys.add(activity);
        LogUtils.d("activityList:size:" + mActivitys.size());
    }


    public void popActivity(Activity activity) {
        mActivitys.remove(activity);
        LogUtils.d( "activityList:size:" + mActivitys.size());
    }


    public static Activity currentActivity() {
        if (mActivitys == null || mActivitys.isEmpty()) {
            return null;
        }
        return mActivitys.get(mActivitys.size() - 1);
    }


    public static void finishCurrentActivity() {
        if (mActivitys == null || mActivitys.isEmpty()) {
            return;
        }
        Activity activity = mActivitys.get(mActivitys.size() - 1);
        finishActivity(activity);
    }


    public static void finishActivity(Activity activity) {
        if (mActivitys == null || mActivitys.isEmpty()) {
            return;
        }
        if (activity != null) {
            mActivitys.remove(activity);
            activity.finish();
            activity = null;
        }
    }


    public static void finishActivity(Class<?> cls) {
        if (mActivitys == null || mActivitys.isEmpty()) {
            return;
        }
        for (Activity activity : mActivitys) {
            if (activity.getClass().equals(cls)) {
                finishActivity(activity);
            }
        }
    }


    public static Activity findActivity(Class<?> cls) {
        Activity targetActivity = null;
        if (mActivitys != null) {
            for (Activity activity : mActivitys) {
                if (activity.getClass().equals(cls)) {
                    targetActivity = activity;
                    break;
                }
            }
        }
        return targetActivity;
    }


    public Activity getTopActivity() {
        Activity mBaseActivity = null;
        synchronized (mActivitys) {
            final int size = mActivitys.size() - 1;
            if (size < 0) {
                return null;
            }
            mBaseActivity = mActivitys.get(size);
        }
        return mBaseActivity;

    }


    public String getTopActivityName() {
        Activity mBaseActivity = null;
        synchronized (mActivitys) {
            final int size = mActivitys.size() - 1;
            if (size < 0) {
                return null;
            }
            mBaseActivity = mActivitys.get(size);
        }
        return mBaseActivity.getClass().getName();
    }


    public static void finishAllActivity() {
        if (mActivitys == null) {
            return;
        }
        for (Activity activity : mActivitys) {
            activity.finish();
        }
        mActivitys.clear();
    }



    /**
     * 退出应用程序
     */
    public static void appExit() {
        try {
            finishAllActivity();

            android.os.Process.killProcess(android.os.Process.myPid());
            System.exit(0);
        } catch (Exception e) {
        }
    }


    private void registerActivityListener() {

        /*
        * 移除了一个if 语句 因为该版本高于判断语句的
        * Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH
        * */

        registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
            @Override
            public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
                /**
                 *  监听到 Activity创建事件 将该 Activity 加入list
                 */
                pushActivity(activity);
                LogUtils.life(activity.getClass().getName(),"on create");
            }

            @Override
            public void onActivityStarted(Activity activity) {

               LogUtils.life(activity.getClass().getName(),"on start");
            }

            @Override
            public void onActivityResumed(Activity activity) {

                LogUtils.life(activity.getClass().getName(),"on resume");

            }

            @Override
            public void onActivityPaused(Activity activity) {
                LogUtils.life(activity.getClass().getName(),"on pause");
            }

            @Override
            public void onActivityStopped(Activity activity) {
                LogUtils.life(activity.getClass().getName(),"on stop");
            }

            @Override
            public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
                LogUtils.life(activity.getClass().getName(),"on save");
            }

            @Override
            public void onActivityDestroyed(Activity activity) {

                if(null !=mActivitys && !mActivitys.isEmpty()){
                    //移除改act
                    popActivity(activity);
                    LogUtils.life(activity.getClass().getName(),"onDestroyed");
                }

            }
        });
    }


}
